import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, MoreThanOrEqual } from 'typeorm';
import { Tag } from '../tags/entities/tag.entity';
import { Text } from '../texts/entities/text.entity';

@Injectable()
export class StatsService {
  constructor(
    @InjectRepository(Tag)
    private readonly tagRepository: Repository<Tag>,
    @InjectRepository(Text)
    private readonly textRepository: Repository<Text>,
  ) {}

  async getStats() {
    const tagCount = await this.tagRepository.count();
    const textCount = await this.textRepository.count();
    
    const sevenDaysAgo = new Date();
    sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
    
    const recentTexts = await this.textRepository.count({
      where: {
        createdAt: MoreThanOrEqual(sevenDaysAgo),
      },
    });

    return {
      tagCount,
      textCount,
      recentTexts,
    };
  }
}